package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.Constant;
import dao.SinhVienDAO;
import model.SinhVienModel;
import util.StringUtil;

public class SinhVienController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SinhVienController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String actionType = request.getParameter("actionType");
		
		if(actionType.equalsIgnoreCase("capnhat")){
			updateSinhVienByMaSV(request, response);
		}
		else if(actionType.equalsIgnoreCase("themmoi")){
			insertSinhVien(request, response);
		}
		else if(actionType.equalsIgnoreCase("sinhVien"))
		{
			SinhVien(request,response);
		}
	}	
	
	
	private void updateSinhVienByMaSV(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		SinhVienModel sinhVienModel=new SinhVienModel();
		sinhVienModel.setMaSV(request.getParameter("txtMaSinhVien").trim());
		sinhVienModel.setTenSV(StringUtil.toUTF8(request.getParameter("txtTenSinhVien").trim()));
		sinhVienModel.setNgaySinh(request.getParameter("txtNgaySinh").trim());
		sinhVienModel.setGioiTinh(request.getParameter("txtGioiTinh").trim());
		sinhVienModel.setDiaChi(StringUtil.toUTF8(request.getParameter("txtDiaChi").trim()));
				
		if(SinhVienDAO.updateSinhVienByMaSV(sinhVienModel))
			response.sendRedirect("danhsachsinhvien.jsp?errUpdate=false"+request.getParameter("txtProLink"));
		else
			response.sendRedirect("danhsachsinhvien.jsp?errUpdate=true"+request.getParameter("txtProLink"));
			
	}
	
	private void insertSinhVien(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		SinhVienModel sinhVienModel=new SinhVienModel();
		sinhVienModel.setMaSV(request.getParameter("txtMaSinhVien").trim());
		sinhVienModel.setTenSV(StringUtil.toUTF8(request.getParameter("txtTenSinhVien").trim()));
		sinhVienModel.setNgaySinh(request.getParameter("txtNgaySinh").trim());
		sinhVienModel.setGioiTinh(request.getParameter("txtGioiTinh").trim());
		sinhVienModel.setDiaChi(StringUtil.toUTF8(request.getParameter("txtDiaChi").trim()));
		
		if(SinhVienDAO.insertSinhVien(sinhVienModel))
			response.sendRedirect("danhsachsinhvien.jsp?errInsert=false"+request.getParameter("txtProLink"));
		else
			response.sendRedirect("danhsachsinhvien.jsp?errInsert=true"+request.getParameter("txtProLink"));
			
	}
	
	protected void SinhVien(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String maSinhVien = "";
		String pageNext = "danhsachsinhvien.jsp?MaSV=" + maSinhVien ;
		RequestDispatcher rd = request.getRequestDispatcher(pageNext);	
		
		ArrayList<SinhVienModel> sinhVienModelList1  = new ArrayList<SinhVienModel>();
		ArrayList<SinhVienModel> sinhVienModelList2  = new ArrayList<SinhVienModel>();
		if(request.getSession().getAttribute("sinhVienList") != null)
			sinhVienModelList1 = (ArrayList<SinhVienModel>)request.getSession().getAttribute("sinhVienList");
		
		for(Integer i = 1; i <= sinhVienModelList1.size(); i++){
			
			SinhVienModel sinhVienModel = sinhVienModelList1.get(i-1);
			
			if(request.getParameter("xoaSV") != null && request.getParameter("xoaSV").equals("xoa") && request.getParameter("chk" + i.toString()) != null)
			{
				if(sinhVienModel.getMaSV() != null){
					SinhVienDAO.deleteSinhVien(sinhVienModel.getMaSV());
				}
			}
			else{
				sinhVienModelList2.add(sinhVienModel);
			}
		}
		
		if(request.getParameter("xoaSV").equals("xoa"))
		{
			
			rd = request.getRequestDispatcher(pageNext);	
			pageNext += "?xoa=DeleteTC";
		}
		request.setAttribute(Constant.SINH_VIEN_MODEL_LIST, sinhVienModelList2);
		rd.forward(request, response);
	}
}
